package com.leetcode.partition3;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author `RKC`
 * @date 2021/8/24 10:13
 */
public class LC300最长递增子序列 {

    public static int lengthOfLIS(int[] nums) {
//        return memoryRecursion(nums, 0, new ArrayList<>(), new HashMap<>());
        return dynamicProgramming(nums);
    }

    public static void main(String[] args) {
        int[] nums = {3, 5, 6, 2, 5, 4};
//        int[] nums = {0, 1, 0, 3, 2};
        System.out.println(lengthOfLIS(nums));
    }

    private static int dynamicProgramming(final int[] nums) {
        //dp[i]表示i之前包括i的最长上升子序列
        int[] dp = new int[nums.length];
        int answer = 0;
        for (int i = 0; i < nums.length; i++) {
            dp[i] = 1;
            for (int j = 0; j < i; j++) {
                //看i之前的元素是否有比nums[i]小的
                if (nums[i] > nums[j]) dp[i] = dp[j] + 1;
            }
            if (answer < dp[i]) answer = dp[i];
        }
        System.out.println(Arrays.toString(dp));
        return answer;
    }

    private static int memoization(final int[] nums, int startIndex, List<Integer> path, Map<List<Integer>, Integer> cache) {
        if (startIndex >= nums.length) {
            System.out.println(path);
            return path.size();
        }
        if (cache.containsKey(path)) return cache.get(path);
        int res = 0;
        for (int i = startIndex; i < nums.length; i++) {
            if (i == nums.length - 1) res = Math.max(res, path.size());
            //不满足递增子序列的进行剪枝
            if (path.size() > 0 && nums[i] <= path.get(path.size() - 1)) continue;
            path.add(nums[i]);
            res = Math.max(memoization(nums, i + 1, path, cache), res);
            path.remove(path.size() - 1);
            cache.put(path, res);
        }
        return res;
    }
}
